/**
 * @description works router
 */

const router = require('koa-router')()
const { worksIdsSchema } = require('../validator/works')
const genValidator = require('../middleware/genValidator')
const { getWorksList, forceOffline, undoForceOffline, getCount, getMonthlyCount } = require('../controller/works')

// 路由前缀
router.prefix('/api/works')

// 获取作品列表
router.get('/', async ctx => {
  const { keyword = '', pageIndex, pageSize } = ctx.query
  const res = await getWorksList(decodeURIComponent(keyword), pageIndex, pageSize)
  ctx.body = res
})

// 强制下线
router.post('/forceOffline', genValidator(worksIdsSchema), async ctx => {
  const { ids } = ctx.request.body
  const res = await forceOffline(ids)
  ctx.body = res
})

// 恢复强制下线
router.post('/undoForceOffline', genValidator(worksIdsSchema), async ctx => {
  const { ids } = ctx.request.body
  const res = await undoForceOffline(ids)
  ctx.body = res
})

// 获取发布和创建的 count
router.get('/getCount', async ctx => {
  const res = await getCount()
  ctx.body = res
})

// 按月，获取创建和发布的数量
router.get('/getMonthlyCount', async ctx => {
  const res = await getMonthlyCount()
  ctx.body = res
})

module.exports = router
